<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CalendarUtils
 *
 * @author virtual
 */
class CalendarUtils {
    public static function getMonthLabel($month){
        $monthLabel = "";
        switch($month){
            case 1: $monthLabel = "Januari"; break;
            case 2: $monthLabel = "Februari"; break;
            case 3: $monthLabel = "Maart"; break;
            case 4: $monthLabel = "April"; break;
            case 5: $monthLabel = "Mei"; break;
            case 6: $monthLabel = "Juni"; break;
            case 7: $monthLabel = "Juli"; break;
            case 8: $monthLabel = "Augustus"; break;
            case 9: $monthLabel = "September"; break;
            case 10: $monthLabel = "Oktober"; break;
            case 11: $monthLabel = "November"; break;
            case 12: $monthLabel = "December"; break;
        }
        return $monthLabel;
    }
    
    public static function getDateFromKey($key){
         $ydmPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})$/";
        
        preg_match($ydmPattern, $key, $matches);
		if(sizeof($matches) == 4){
			$index = $matches[1];
			return mktime(0, 0, 0, $matches[2], $matches[3], $matches[1]);
		}
    }
    
    public static function getDbDateFromKey($key){
        return date("Y-m-d", self::getDateFromKey($key));
    }
    
    public static function getDbDateToday(){
        return date("Y-m-d");
    }
    
    public static function getYesterdayDateKey() {
        return date('Ymd', mktime(0, 0, 0, self::getThisMonth(), self::getThisDay()-1, self::getThisYear()));
    }
    public static function getTomorrowDateKey() {
        return date('Ymd', mktime(0, 0, 0, self::getThisMonth(), self::getThisDay()+1, self::getThisYear()));
    }
    
    public static function getOffsetFromDateKey($date, $dateoffset){
        
        if($date == "null"){
            
            $date = self::getTodayDateKey();
        }
        
        $ydmPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})$/";
        
        preg_match($ydmPattern, $date, $matches);
		if(sizeof($matches) == 4){
			$index = $matches[1];
			return date("Ymd", mktime(0, 0, 0, $matches[2], $matches[3] + $dateoffset, $matches[1]));
		}
        return null;
    }
    
    public static function getTodayDateKey() {
        $date = new DateTime();
	    return $date->format('Ymd');	
    }
    
    public static function getThisYear() {
        $date = new DateTime();
	    return $date->format('Y');	
    }
    
    public static function getThisMonth() {
        $date = new DateTime();
	    return $date->format('m');	
    }
    
    public static function getThisDay() {
        $date = new DateTime();
	    return $date->format('d');	
    }
    
    //convert a date to a db timestamp
    public static function transformDateToTimestamp($date) {
        if($date != null)
            return $date->format('Y-m-d H:i:s');
        else
            return "";
    }
    
    public static function transformDateToTime($date) {
        if($date != null)
            return $date->format('H:i');
        else {
            return "";
        }
    }
    
    public static function transformDateToDateKey($date) {
        if($date != null){
            return $date->format('Ymd');
        }
        else {
            return "";
        }
    }
    
    
    
    //get time as input and return current date and time given.
    public static function createDateFromTime($time) {
        //create new date time
			$date = new DateTime();
			
			//parse the time
			$timePattern = "/^([0-1][0-9]|2[0-3]):([0-5][0-9])$/";  //24-hr : seconds
			preg_match($timePattern, $time, $matches);
			if(sizeof($matches) == 3){
				$hrs = $matches[1];
				$sec = $matches[2];
							
				$date->setTime($hrs,$sec,0);
				//echo "from parse: " . $date->getTimestamp();
			}
			else {
				//echo "couldn't parse, use default: " . $date->getTimestamp();
				
			}
			return $date;
    }
}

?>
